package ve.com.ucv.changedetection.model;

import org.apache.log4j.Logger;
import ve.com.ucv.changedetection.objects.TreeNode;

import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * User: josmaguirre
 * Date: 5/12/12
 * Time: 8:54
 * To change this template use File | Settings | File Templates.
 */
public class NovellImplementation {
    public TreeNode oldTree, currentTree;
    public boolean differences;
    public ArrayList<String> changes = new ArrayList<String>();
    public Logger logger = Logger.getLogger(NovellImplementation.class);

    public NovellImplementation(){
        this.oldTree = null;
        this.currentTree = null;
        this.differences = false;
    }

    public NovellImplementation(TreeNode oldTree, TreeNode currentTree){
        this.currentTree = currentTree;
        this.oldTree = oldTree;
        this.differences = false;
    }

    public void compareTrees(){
        compareAbstractTrees(this.oldTree, this.currentTree);
    }

    public void compareAbstractTrees(TreeNode oldTree, TreeNode currentTree){
        logger.info("COMPARING TREE NODE " + oldTree.getNodeName() + " OF OLD PAGE TREE WITH " + currentTree.getNodeName() +
                " OF NEW PAGE TREE");
        if (oldTree.getHashValue() != currentTree.getHashValue()){
            if (oldTree.getChildNodes().size() != 0){
                for (int i = 0; i < oldTree.getChildNodes().size(); i++){
                    compareAbstractTrees(oldTree.getChildNodes().get(i), currentTree.getChildNodes().get(i));
                }
            }else{
                logger.info("CHANGE DETECTED:");
                logger.info("OLD TREE NODE [ NODE: " + oldTree.getNodeType() + " VALUE: " + oldTree.getNodeValue() + " ]");
                logger.info("NEW TREE NODE [ NODE: " + currentTree.getNodeType() + " VALUE: " + currentTree.getNodeValue() + " ]");
                currentTree.setChanged(true);
                registerChange(currentTree.getNodeType());
                setDifferences(true);
            }
        }
    }

    public void registerChange(String change){
        this.changes.add(change);
    }

    public TreeNode getOldTree() {
        return oldTree;
    }

    public void setOldTree(TreeNode oldTree) {
        this.oldTree = oldTree;
    }

    public TreeNode getCurrentTree() {
        return currentTree;
    }

    public void setCurrentTree(TreeNode currentTree) {
        this.currentTree = currentTree;
    }

    public boolean isDifferences() {
        return differences;
    }

    public void setDifferences(boolean differences) {
        this.differences = differences;
    }

    public ArrayList<String> getChanges() {
        return changes;
    }

    public void setChanges(ArrayList<String> changes) {
        this.changes = changes;
    }
}
